using System;
using System.Windows;
using ChessKit.Common;

namespace ChessKit.Ui.ChessPanel
{
  internal class SlaveLayout : ISlaveLayout
  {
    private double _cellSize;

    public Rect this[BoardLocation location]
    {
      get
      {
        switch (location.Area)
        {
          case BoardArea.Cell:
            return new Rect((location.X-1) * _cellSize,
                            (location.Y-1) * _cellSize, _cellSize, _cellSize);
          default:
            return new Rect(DesiredSize);
        }
      }
    }

    public Size DesiredSize { get; private set; }

    public void SlaveMeasure(Size availableSize)
    {
      var a = Math.Min(availableSize.Width, availableSize.Height);
      DesiredSize = double.IsInfinity(a) || double.IsNaN(a) ? new Size() : new Size(a, a);
    }

    public void SlaveArrange(Size arrangeSize)
    {
      var a = Math.Min(arrangeSize.Width, arrangeSize.Height);
      _cellSize = a / 8;
      DesiredSize = new Size(a, a);
    }
  }
}